package com.pipe.protocol.query;

import com.pipe.util.MysqlProtocolUtil;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Kiven
 */
public class TableMetaInfo {
    private String tableName;
    private String dataBaseName;
    private List<ColumnDefinition41Package> columnDefinitionList = new LinkedList<>();
    private List<ResultsetRowPacakge> resultList = new LinkedList<>();
    private List<Integer> columnLengthList = new ArrayList<>(20);

    public TableMetaInfo(String dataBaseName, String tableName) {
        this.dataBaseName = dataBaseName;
        this.tableName = tableName;
    }

    public TableMetaInfo(List<ColumnDefinition41Package> columnDefinitionList, List<ResultsetRowPacakge> resultList) {
        this.columnDefinitionList = columnDefinitionList;
        this.resultList = resultList;
    }

    public void build(List<ColumnDefinition41Package> columnDefinitionList, List<ResultsetRowPacakge> resultList) {
        this.columnDefinitionList = columnDefinitionList;
        this.resultList = resultList;
        buildColumnInfo(resultList);
    }

    public String getTableName() {
        return tableName;
    }

    public String getDataBaseName() {
        return dataBaseName;
    }

    private void buildColumnInfo(List<ResultsetRowPacakge> resultList) {
        for (ResultsetRowPacakge resultsetRow : resultList) {
            String type = resultsetRow.getValue().get(1);
            int typeLen = MysqlProtocolUtil.getLengthFromTypeString(type);
            columnLengthList.add(typeLen);
        }
    }

    public List<ResultsetRowPacakge> getResultList() {
        return resultList;
    }

    public List<ColumnDefinition41Package> getColumnDefinitionList() {
        return columnDefinitionList;
    }

    public List<Integer> getColumnLengthList() {
        return columnLengthList;
    }

    @Override
    public String toString() {
        return "TableMetaInfo{" +
                "tableName='" + tableName + '\'' +
                ", columnDefinitionList=" + columnDefinitionList +
                ", resultList=" + resultList +
                '}';
    }
}